﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Crow.Library.Logger.PerformanceCounting
{
    public sealed class Performance : IDisposable
    {
        public static readonly List<Counter> Counters = new List<Counter>();
        private readonly string _key;
        private readonly Stopwatch watch = new Stopwatch();

        public Performance(string key)
        {
            watch.Start();
            _key = key;
        }

        #region IDisposable Members

        public void Dispose()
        {
            watch.Stop();
            Counters.Add(new Counter
                             {
                                 Duration = watch.ElapsedMilliseconds,
                                 Key = _key
                             });
        }

        #endregion

        public static void WriteAndClear(StringBuilder builder)
        {
            foreach (Counter item in Counters)
            {
                builder.AppendFormat("{0} : {1} ms.", item.Key, item.Duration);
            }
            Counters.Clear();
        }
    }
}